package com.tjetc.view.teacherview;

import cn.hutool.core.date.DateTime;
import cn.hutool.json.JSONUtil;
import com.tjetc.proxy.CilentProxy;
import com.zhiyun.domain.*;
import com.zhiyun.service.ExaminationService;
import com.zhiyun.service.StudentService;
import com.zhiyun.service.TestPaperService;
import com.zhiyun.vo.Result;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Scanner;

public class MyCourseView_In_Exams_MarkExam {
    StudentService studentService=CilentProxy.getProxyInstance(StudentService.class);
    ExaminationService examinationService=CilentProxy.getProxyInstance(ExaminationService.class);
    TestPaperService testPaperService= CilentProxy.getProxyInstance(TestPaperService.class);
    Scanner scanner=new Scanner(System.in);

    public void start(Examination examination) {
        while (true){
            System.out.println("~~~~~~考试详情~~~~~");
            System.out.println("1.修改考试信息");
            System.out.println("2.查看所有学生考试记录");
            System.out.println("3.批改试卷");
            System.out.println("0.返回上一级");
            int i = scanner.nextInt();
            switch (i){
                case 1:
                    update(examination);
                    break;
                case 2:
                    findall(examination);
                    break;
                case 3:
                    selectById(examination);
                    break;
                case 0:
                    return;
                default:
                    System.out.println("输入有误,请重新输入~~~");
            }
        }
    }

    private void selectById(Examination examination) {
        while (true){
            Result allStudentDetails = examinationService.getAllStudentDetails(examination.getId());
            if (allStudentDetails.getCode()==200){
                List<ExaminationRecord> allStudentDetailsObj =
                        (List<ExaminationRecord>) allStudentDetails.getObj();
                for (ExaminationRecord examinationRecord : allStudentDetailsObj) {
                    Result result = studentService.myInfo(examinationRecord.getStudentId());
                    if (result.getCode()==200){
                        Student obj = examinationRecord.getStudent();

                           System.out.println("学生id "+obj.getId()+ "  学生:  "+obj.getTruename()+ "   成绩: "+(examinationRecord.getScore()==null?"未考试":examinationRecord.getScore()));

                    }
                }
                boolean a=false;
                System.out.println("请选择想要查看的学生试卷(-1退出):");
                int i = scanner.nextInt();
                if (i==-1){
                    return;
                }
                for (ExaminationRecord examinationRecord : allStudentDetailsObj) {


                        if (i==examinationRecord.getStudent().getId()&&examinationRecord.getScore()!=null){
                            a=true;
                            break;
                        }else {
                            System.out.println("考生未答题");
                            return;
                        }

                }
                if (a){
                    Result examinationDetails = examinationService.getExaminationDetails(examination.getId(), i);
                    if (examinationDetails.getCode()==200){
                        ExaminationRecord obj = (ExaminationRecord) examinationDetails.getObj();
                        List<Response> responses = obj.getResponses();
                        for (Response respons : responses) {
                            TestQuestion testQuestion = respons.getTestQuestion();
                            Integer typeId = testQuestion.getTypeId();
                            if (typeId==4){
                                System.out.println("题目:   "+testQuestion.getTitle());
                                System.out.println("正确答案:  "+ JSONUtil.toJsonStr(testQuestion.getAnswer()));
                                System.out.println("学生答案:  "+ JSONUtil.toJsonStr(respons.getContent()));
                                while (true){
                                    System.out.println("请给出分数");
                                    int i1 = scanner.nextInt();
                                    if (i1>=0&&i1<=testQuestion.getScore()){
                                        respons.setScore(i1);
                                        Result result = examinationService.updateResponseScore(respons.getId(), i1);
                                        System.out.println(result.getMsg());
                                    }else {
                                        continue;
                                    }
                                    break;
                                }
                            }
                        }
                    }
                }else {
                    System.out.println("您想查看的学生不存在");
                }

            }
            break;
        }
    }

    private void findall(Examination examination) {
        while (true){
            Result allStudentDetails = examinationService.getAllStudentDetails(examination.getId());
            if (allStudentDetails.getCode()==200){
                List<ExaminationRecord> allStudentDetailsObj =
                        (List<ExaminationRecord>) allStudentDetails.getObj();
                for (ExaminationRecord examinationRecord : allStudentDetailsObj) {
                    Result result = studentService.myInfo(examinationRecord.getStudentId());
                    if (result.getCode()==200){
                        Student obj = examinationRecord.getStudent();

                           System.out.println(obj.getId()+"  学生:  "+obj.getTruename()+ "   成绩: "+(examinationRecord.getScore()==null?"未考试":examinationRecord.getScore()));

                    }

                }
                while (true){
                    System.out.println("是否查看某个学生的试卷(Y/N):");
                    String next = scanner.next();
                    if (next.equals("Y")){
                        boolean a=false;
                        System.out.println("请选择想要查看的学生试卷:");
                        int i = scanner.nextInt();
                        for (ExaminationRecord examinationRecord : allStudentDetailsObj) {
                            Result result = studentService.myInfo(examinationRecord.getStudentId());
                            if (result.getCode()==200){
                                Student obj = examinationRecord.getStudent();
                                if (i==obj.getId()){
                                    a=true;
                                    break;
                                }
                            }
                        }
                        if (a){
                            Result examinationDetails = examinationService.getExaminationDetails(examination.getId(), i);
                            if (examinationDetails.getCode()==200){
                                ExaminationRecord obj = (ExaminationRecord) examinationDetails.getObj();
                                List<Response> responses = obj.getResponses();
                                for (Response respons : responses) {
                                    TestQuestion testQuestion = respons.getTestQuestion();
                                    Integer typeId = testQuestion.getTypeId();
                                    if (typeId==4){
                                        System.out.println("题目:   "+testQuestion.getTitle());
                                        System.out.println("正确答案:  "+ JSONUtil.toJsonStr(testQuestion.getAnswer()));
                                        System.out.println("学生答案:  "+ JSONUtil.toJsonStr(respons.getContent()));
                                    }
                                }



                            }


                        }
                    }else if (next.equals("N")){
                        break;
                    }else {
                        System.out.println("输入错误,请重新输入");
                    }
                }
            }
            break;
        }
    }

    private void update(Examination examination) {
        while (true){
            System.out.println("请输入修改后的标题");
            String name = scanner.next();
            System.out.println("请输入修改后的考试描述");
            String desc = scanner.next();
            System.out.println("请输入修改后开始时间:");
            System.out.println("年:");
            int i0 = scanner.nextInt();
            System.out.println("月:");
            int i1 = scanner.nextInt();
            System.out.println("日:");
            int i2 = scanner.nextInt();
            System.out.println("请输入修改后结束时间:");
            DateTime dateTime = new DateTime(LocalDateTime.of(i0, i1, i2, 0, 0, 0));
            System.out.println("年:");
            int j = scanner.nextInt();
            System.out.println("月:");
            int j1 = scanner.nextInt();
            System.out.println("日:");
            int j2 = scanner.nextInt();
            DateTime dateTime2 = new DateTime(LocalDateTime.of(j, j1, j2, 0, 0, 0));
            System.out.println("请输入题目是否乱序(1乱序0不乱序)");
            int i = scanner.nextInt();
            System.out.println("请输入选项是否乱序(1乱序0不乱序)");
            int i3 = scanner.nextInt();
            examination.setName(name);
            examination.setDesc(desc);
            examination.setStarttime(dateTime);
            examination.setEndtime(dateTime2);
            examination.setTitleOrder(i);
            examination.setOptionOrder(i3);
            Result result = examinationService.updateExamination(examination);
            System.out.println(result.getMsg());
            break;
        }
    }
}
